<?php
/**
 * Created by CuiLiwu.
 * User: CuiLiwu
 * Date: 2018/4/18
 * Time: 14:54
 */

namespace App\Http\Controllers\Web\Member\Buyer;

use Illuminate\Http\Request;
use App\Http\Controllers\Web\BaseController;



class CollectionController extends BaseController
{
    protected $api_url = [
        'index'  => ['get',   'buyer/collection'],
        'show'   => ['get',   'buyer/collection/%d'],
        'store'  => ['post',  'buyer/collection'],
        'update' => ['put',   'buyer/collection/%d'],
        'delete' => ['delete','buyer/collection/%d'],
        'multiDelete' => ['post','buyer/collection/multiDelete']
    ];

    protected $api_host;

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_web');
    }

    public function index()
    {
        $search_params = $this->request->all(['type']);
        $search_params['type'] = $search_params['type']?$search_params['type']:1; // 默认商品收藏
        $page = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'page' => $page,
            'with' => 'sku,product,shop'
        ];
        //请求部分-收藏列表
        $collection = http_web($this->api_url['index'])->send($params);

        $data= paginate($this->request, $collection['data']);
        $data['search_params'] = $search_params;
        return view('web.user.buyer.attention', $data);
    }
    /**
     * 新增页面
     * method get
     * url /create
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        // 无新增页
    }

    public function show()
    {
        // 无详情页
    }

    /**
     * 创建、修改 收藏-form提交
     * method post
     * url /
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function store()
    {
        //验证部分
        $data = $this->form();
        $id = $this->request->get('id',null);

        //请求部分
        $return = http_web($this->api_url['store'])->send($data);
        return response()->json(['code'=>$return['code'],'message'=>$return['message']]);
    }

    public function edit($id)
    {
        // 无编辑
    }
    // 更新 默认
    public function update($id,$shop_id)
    {
        //请求部分
        $ret = http_web($this->api_url['update'], [$id,$shop_id])->send();

        //结果处理部分
        return $ret;
    }

    //update公共方法
    private function ajaxUpdate($id, $data)
    {
        $ret = http_web($this->api_url['store'],[$id])->send($data);

        return $ret;
    }

    public function destroy($id)
    {
        $ret = http_web($this->api_url['delete'],[$id])->send();
        return $ret;
    }

    public function multiDelete(Request $request){
        try{
            $data = $request->except('_token','_method');
            $ret = http_web($this->api_url['multiDelete'])->send($data);
        }catch (\Exception $e){
            return $e->getMessage();
        }
        return $ret;

    }
    /**
     * 创建、更新公共验证部分
     * @return array
     */
    private function form()
    {
        $this->validate($this->request, [
            'shop_id'   => 'integer',
            'user_id'   => 'required|integer',
            'product_id'=> 'integer',
            'sku_id'    => 'integer',
            'type'      => 'integer|in:1,2',
            'is_invalid'=> 'integer|in:1,2',
        ],[
            'shop_id'   => '店铺id必须为数字类型',
            'user_id'   => '无用户id|用户id',
            'product_id'=> 'product_id必须为数字',
            'sku_id'    => 'sku_id 必须为数字',
            'type'      => 'type必须为数字|type必须为1或2',
            'is_invalid'=> 'integer|is_invalid必须为1或2',
        ]);
        return $this->request->all();
    }
}